<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .destination-container {
            display: flex;
            margin-bottom: 20px;
            background-color: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 1000px; /* 你可以设置一个合适的宽度 */
            height: 250px; /* 你可以设置一个合适的高度 */
            margin: 20px auto;
        }


        .destination-image {
            width: 50%;
            height: auto;
            object-fit: cover;
        }

        .destination-info {
            width: 50%;
            padding: 20px;
        }

        h2, p {
            margin: 0;
        }

        #wrap1, #wrap2 {
            display: flex;
            justify-content: flex-start;
        }

        #tagChoose {
            margin-right: 10px; /* 你可以根据需要调整间隔大小 */
        }

        /* 可以根据需要设置 #tagChoose 的样式 */
        #tagChoose a {
            text-decoration: none;
            color: black;
        }

        #tagChoose div {
            border: 1px solid #ddd;
            padding: 10px;
        }

        #locationChoose {
            margin-right: 10px; /* 你可以根据需要调整间隔大小 */
        }

        /* 可以根据需要设置 #tagChoose 的样式 */
        #locationChoose a {
            text-decoration: none;
            color: black;
        }

        #locationChoose div {
            border: 1px solid #ddd;
            padding: 10px;
        }


        .breadcrumb {
            list-style: none;
            display: flex;
            padding: 0;
        }

        .breadcrumb-item {
            margin-right: 5px;
            position: relative;
        }

        .breadcrumb-item a {
            text-decoration: none;
            color: #3498db;
        }

        .breadcrumb-item button {
            background: none;
            border: none;
            color: #e74c3c;
            cursor: pointer;
            font-size: inherit;
            margin-left: 5px;
            display: inline-block;
        }


        .bar7 {
            background: #7BA7AB;
        }

        .bar7 form {
            height: 42px;
        }

        .bar7 input {
            width: 250px;
            border-radius: 42px;
            border: 2px solid #324B4E;
            background: #F9F0DA;
            transition: .3s linear;
            float: right;
        }

        .bar7 input:focus {
            width: 300px;
        }

        .bar7 button {
            background: none;
            top: -2px;
            right: 0;
        }

        .bar7 button:before {
            content: "\f002";
            font-family: FontAwesome;
            color: #324b4e;
        }


        form {
            position: relative;
            width: 300px;
            margin: 0 auto;
        }

        input, button {
            border: none;
            outline: none;
        }

        input {
            width: 100%;
            height: 42px;
            padding-left: 13px;
        }

        button {
            height: 42px;
            width: 42px;
            cursor: pointer;
            position: absolute;
        }

        #keyword_input, a {
            display: inline;
        }

        
        .sort-button {
            display: inline-block;
            padding: 5px 10px;
            background-color: #f1f1f1;
            border: none;
            cursor: pointer;
            position: relative; /* 添加相对定位 */
            width: 150px; /* 设置按钮宽度 */
            height: 30px; /* 设置按钮高度 */
            text-align: center; /* 按钮文本居中 */
            font-size: 14px; /* 设置字体大小 */
        }

        .arrow-up,
        .arrow-down {
            width: 0;
            height: 0;
            display: inline-block;
            border: solid transparent;
            content: "";
            pointer-events: none;
            position: absolute;
        }

        .arrow-up {
            border-top-color: #c21c1c;
            border-width: 6px 4px 0 4px;
            top: 50%; /* 相对父元素垂直居中 */
            transform: translateY(-50%); /* 垂直居中 */
        }

        .arrow-down {
            border-bottom-color: #c21c1c;
            border-width: 0 4px 6px 4px;
            bottom: 50%; /* 相对父元素垂直居中 */
            transform: translateY(50%); /* 垂直居中 */
        }

        .search {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 100px; /* 调整垂直位置 */
        }

        /* 顶部导航栏样式 */
        nav {
            background-color: #fff;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            z-index: 99;
            text-align: center; /* 居中显示 */
            display: flex;
            align-items: center;
            padding: 10px 20px;
        }

        nav img {
            height: 30px;
        }

        nav a {
            display: inline-block;
            color: #555;
            text-align: center;
            padding: 10px 60px; /* 调整导航栏每个元素的大小 */
            text-decoration: none;
        }

        nav a:hover {
            color: #000;
            background-color: #f5f5f5;
        }

        nav a.active {
            color: #4caf50;
            background-color: #fff;
        }

        .dropdown {
            position: relative;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            top: 100%;
            width: 150px;
            background-color: #f9f9f9;
            padding: 10px;
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        }

        .dropdown-content a {
            color: black;
            padding: 6px 16px;
            text-decoration: none;
            display: block;
        }

        .dropdown:hover .dropdown-content,
        .dropdown-content:hover {
            display: block;
        }
        /* 顶部导航栏样式 */
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<nav>
    <div class="dropdown">
        <a href="http://sight.travel.com/">
            <img src="https://wen-travel.oss-cn-guangzhou.aliyuncs.com/logo.jpg" alt="Logo">
        </a>
    </div>
    <div class="dropdown">
        <a href="http://sight.travel.com/theme">主题精选</a>
    </div>
    <div class="dropdown">
        <a href="#">联系我们</a>
    </div>
    <div class="dropdown">
        <a href="#">我的</a>
        <div class="dropdown-content">
            <a href="#">我的账号</a>
            <a href="http://member.travel.com/memberOrder.html">我的订单</a>
            <a href="#">我的地址</a>
        </div>
    </div>
    <a th:if="${loginUser != null}">欢迎, [[${loginUser.username}]]</a>
    <a th:if="${loginUser == null}" href="http://auth.travel.com/login.html">你好，请登录</a>
    <a th:if="${loginUser == null}" href="http://auth.travel.com/regist.html" class="li_2">免费注册</a>
    <a th:if="${loginUser != null}" href="http://auth.travel.com/loguot.html" class="li_2">退出</a>
</nav>


<!-- 搜索栏 -->
<div class="search">
    <input id="keyword_input" th:value="${result.getKeyword()}" type="text" placeholder="输入目的地..." style="width: 400px; height: 50px;font-size: 18px;"/>
    <img src="/image/search.png" onclick="searchByKeyWord()" style="margin-left: 10px;">
</div>

<!-- 遍历面包屑功能 -->
<ul class="breadcrumb">
    <li class="breadcrumb-item"><a href="">关键词：[[${result.getKeyword()}]]</a></li>
    <li class="breadcrumb-item" th:each="nav:${result.navs}">
        <div>
            <span th:text="${nav.navName}+':'" style="color: #3498db"></span>
            <span th:text="${nav.navValue}" style="color: #3498db"></span>
            <a th:href="${nav.link}" style="color: #e74c3c">×</a>
        </div>
    </li>
</ul>

<!-- 景点筛选和排序 -->
<!-- 景点标签 -->
<div>
    <div id="wrap1">
        <li th:each="tag:${result.tags}" style="list-style-type:none">
            <!-- 调用searchProducts方法 在原来的url路径上拼接上 tag=xxx 进行筛选 -->
            <div id="tagChoose">
                <a th:href="${'javascript:searchAttractions(&quot;tag&quot;,&quot;'+tag+'&quot;)'}">
                    <div th:text="${tag}">
                        自然风景
                    </div>
                </a>
            </div>
        </li>
    </div>

    <!-- 省份 -->
    <div id="wrap2">
        <li th:each="location:${result.locations}" style="list-style-type:none">
            <!-- 调用searchProducts方法 在原来的url路径上拼接上 location=xxx 进行筛选 -->
            <div id="locationChoose">
                <a th:href="${'javascript:searchAttractions(&quot;location&quot;,&quot;'+location+'&quot;)'}">
                    <div th:text="${location}">
                        广东
                    </div>
                </a>
            </div>
        </li>
    </div>
</div>

<!-- 排序 -->
<div id="buttons">
    <button class="sort-button" data-sort="synthesis" onclick="toggleSort('synthesis', this)">
        <span class="text">综合排序</span>
        <span class="arrow arrow-up"></span>
    </button>
    <button class="sort-button" data-sort="hotScore" onclick="toggleSort('hotScore', this)">
        <span class="text">热度</span>
        <span class="arrow arrow-up"></span>
    </button>
    <button class="sort-button" data-sort="score" onclick="toggleSort('score', this)">
        <span class="text">评分</span>
        <span class="arrow arrow-up"></span>
    </button>
</div>


<!-- 景点列表 -->
<div th:each="attraction : ${result.getAttractions()}" class="destination-container"
     th:onclick="'window.open(\'http://sight.travel.com/' + ${attraction.attractionId} + '.html\', \'_blank\')'">
    <img class="destination-image" th:src="${attraction.defaultImageUrl}" alt="景点图片">
    <div class="destination-info">
        <h2 th:utext="${attraction.name}">景点名称</h2>
        <p th:text="${attraction.introduction}">景点的信息</p>
    </div>
</div>

</body>

<script type="text/javascript">



    // url参数 添加/替换
    function replaceParamVal(url, paramName, replaceVal, forceAdd) {
        var oUrl = url.toString();
        var nUrl;
        if (oUrl.indexOf(paramName) != -1) {
            if (forceAdd) {
                if (oUrl.indexOf("?") != -1) {
                    nUrl = oUrl + "&" + paramName + "=" + replaceVal;
                } else {
                    nUrl = oUrl + "?" + paramName + "=" + replaceVal;
                }
            } else {
                var re = eval('/(' + paramName + '=)([^&]*)/gi');
                nUrl = oUrl.replace(re, paramName + '=' + replaceVal);
            }
        } else {
            if (oUrl.indexOf("?") != -1) {
                nUrl = oUrl + "&" + paramName + "=" + replaceVal;
            } else {
                nUrl = oUrl + "?" + paramName + "=" + replaceVal;
            }
        }
        return nUrl;
    };

    function searchAttractions(name, value) {
        //原来的页面的url：location.href
        var url = replaceParamVal(location.href, name, value, true);
        // window.open(url);
        window.location.href = url;
    }

    // 关键词检索
    function searchByKeyWord() {
        var currentKeyword = $("#keyword_input").val();

        // 检查搜索栏是否有关键词
        if (currentKeyword.trim() === '') {
            // 如果搜索栏没有关键词，则跳转至指定 URL
            window.location.href = 'http://search.travel.com/attractionList.html?keyword=';
        } else {
            var currentUrl = location.href;

            // 检查当前 URL 是否包含 keyword 参数
            if (currentUrl.indexOf("keyword") !== -1) {
                // 如果包含，则替换现有的 keyword 参数值
                var newUrl = replaceParamVal(currentUrl, "keyword", currentKeyword, false);
                window.location.href = newUrl;
            } else {
                // 如果不包含，则直接添加 keyword 参数
                var url = replaceParamVal(currentUrl, "keyword", currentKeyword, true);
                window.location.href = url;
            }
        }
    }


    // 获取当前页面的 URL
    var currentURL = window.location.href;
    // 检查 URL 中是否包含名为 "tag" 的参数
    if (currentURL.includes('tag')) {
        // 如果 URL 中包含 "tag" 参数，就隐藏相关的元素
        var tagChoose = document.getElementById('tagChoose');
        tagChoose.style.display = 'none';
    }
    // 检查 URL 中是否包含名为 "location" 的参数
    if (currentURL.includes('location')) {
        // 如果 URL 中包含 "location" 参数，就隐藏相关的元素
        var locationChoose = document.getElementById('locationChoose');
        locationChoose.style.display = 'none';
    }

    // 点击触发排序事件
    function toggleSort(sortType, button) {
        const arrow = button.querySelector('.arrow');
        const isArrowUp = arrow.classList.contains('arrow-up');
        const currentSort = getParameterByName('sort');


        if (currentSort && currentSort.startsWith(sortType)) {
            // If the current sort is for the same type, toggle direction
            newSort = currentSort.endsWith('_asc') ? `${sortType}_desc` : `${sortType}_asc`;
        } else {
            // If the current sort is for a different type or not present, set as asc
            newSort = `${sortType}_desc`;
        }

        // Update URL and navigate to the new page
        updateUrlParam('sort', newSort);
        window.location.href = buildNewUrl();

        console.log(`Sorting by ${sortType} ${isArrowUp ? 'descending' : 'ascending'}`);
    }

    function getParameterByName(name) {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(name);
    }

    function updateUrlParam(key, value) {
        const url = new URL(window.location);
        url.searchParams.set(key, value);
        window.history.replaceState({}, '', url);
    }


    function buildNewUrl() {
        const url = new URL(window.location);
        const sortParam = getParameterByName('sort');
        if (sortParam) {
            url.searchParams.set('sort', sortParam);
        } else {
            url.searchParams.delete('sort');
        }
        return url.href;
    }

    // 解析当前 URL 中的排序参数
    function getSortParameter() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('sort');
    }

    // 根据排序参数设置箭头方向
    function setArrowDirection() {
        const sortParameter = getSortParameter();
        const buttons = document.querySelectorAll('.sort-button');

        buttons.forEach(button => {
            const dataSort = button.getAttribute('data-sort');
            const arrow = button.querySelector('.arrow');

            // 判断当前排序参数是否匹配按钮的数据排序值
            console.log(dataSort,arrow)
            if (sortParameter && sortParameter.startsWith(dataSort)) {
                if (sortParameter.endsWith('_asc')) {
                    arrow.classList.remove('arrow-up');
                    arrow.classList.add('arrow-down');
                } else if (sortParameter.endsWith('_desc')) {
                    arrow.classList.remove('arrow-down');
                    arrow.classList.add('arrow-up');
                }
            } else {
                arrow.classList.remove('arrow-down');
                arrow.classList.add('arrow-up');
            }
        });
    }



    // 初始化箭头方向
    setArrowDirection();


</script>
</html>